package com.supermap.dataservice.advisoryinfo;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.advisoryinfo.dao.AdvisoryinfoDao;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.advisoryinfo.entity.AdvAllInfo;
import com.supermap.dubbo.api.advisoryinfo.entity.AdvComment;
import com.supermap.dubbo.api.advisoryinfo.entity.Advisoryinfo;
import com.supermap.dubbo.api.advisoryinfo.entity.Advpraise;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.advisoryinfo.AdvisoryinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:
 * @Date:18/1/29 上午9:36
 */
@Service
public class AdvisoryinfoServiceImpl implements AdvisoryinfoService {

    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AdvisoryinfoServiceImpl.class);

    @Resource
    private AdvisoryinfoDao advisoryinfoDao;

    @Override
    public JSONUtil getAdvisoryInfoByUIdArea(String userId,String areaCode) {
            //初始化Json封装工具类
            JSONUtil jsonUtil = new JSONUtil();
            try {
                //获得用户所有的咨询信息
                if(areaCode!=null){
                    areaCode=areaCode+"%";
                }
                List<Advisoryinfo> advisoryinfos = advisoryinfoDao.getAdvisoryInfoByUIdArea(areaCode);
                for(Advisoryinfo adv:advisoryinfos){
                    int advId = adv.getId();
                    String isPraise = advisoryinfoDao.getAdvpraise(Integer.valueOf(userId), advId);
                    if(isPraise==null || isPraise.equals("0")){
                        adv.setIsPraise(0);//未点赞
                    }else{
                        adv.setIsPraise(1);//已点赞
                    }
                }
                //获得用户所有的咨询信息后返回的json数据
                jsonUtil.setMsg("Get user advisoryinfos success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(advisoryinfos);
            }catch (Exception e) {
                //捕捉数据库异常
                logger.error("AdvisoryinfoServiceImpl=>getAdvisoryInfoByUserId=>DaoException:"+e.getMessage());
                jsonUtil.setMsg("Get user advisoryinfos flase！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }
            return jsonUtil;
        }

    @Override
    public JSONUtil addAdvisory(Advisoryinfo advisoryinfo) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //增加咨询信息
            advisoryinfoDao.addAdvisory(advisoryinfo);
            //增加咨询信息后返回的json数据
            jsonUtil.setMsg("Add advisoryinfo success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(advisoryinfo);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>addAdvisory=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Add advisoryinfo flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAdvisoryInfo() {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得所有的咨询信息
            List<Advisoryinfo> advisoryinfos = advisoryinfoDao.getAdvisoryInfo();

            //获得用户所有的咨询信息后返回的json数据
            jsonUtil.setMsg("Get user advisoryinfos success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(advisoryinfos);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>getAdvisoryInfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get user advisoryinfos flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public int getAdvpraiseCount(int advInfoId) {
        return advisoryinfoDao.getAdvpraiseCount(advInfoId);
    }

    @Override
    public boolean getAdvpraise(int userId, int advInfoId) {
       String  advPraise=advisoryinfoDao.getAdvpraise(userId,advInfoId);
       if(advPraise==null){
           return true;
       }
        return false;
    }

    @Override
    public boolean addAdvpraise(Advpraise advpraise) {
       String advPraise= advisoryinfoDao.getAdvpraise(advpraise.getUserId(),advpraise.getAdvInfoId());
        if(advPraise==null ){
           int a= advisoryinfoDao.addAdvpraise(advpraise);
            if(a==1){
                return true;
            }
            return false;
        }else{
           int a2= advisoryinfoDao.DelAdvById(advpraise.getUserId(),advpraise.getAdvInfoId());
           return  false;
        }
    }

    @Override
    public boolean addAdvcomment(AdvComment advComment,String uid) {
        int a=advisoryinfoDao.addAdvcomment(advComment);
        if(a==1){
            //默认为未读评论 并添加到该表
            advisoryinfoDao.addNewMessage(advComment.getUserId(),advComment.getAdvInfoId(),1,advComment.getComment(),0,uid);
            return true;
        }
        return false;
    }

    @Override
    public JSONUtil getAdvCommentListByAdvInfoId(int advInfoId) {
        List<AdvComment> advcomments=new ArrayList<AdvComment>();
        List<AdvComment> list=advisoryinfoDao.getAdvcommentListByAdvInfoId(advInfoId);
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            for(AdvComment a:list){
                if(a.getParentId()==-1)advcomments.add(a);
            }
            getAdvCommentList(list, advcomments);
            int AdcCount=list.size();
            jsonUtil.setMsg("getAdvCommentCount:"+AdcCount);
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(advcomments);
        }catch (Exception e){
            logger.error("AdvServiceImpl=>getAdvCommentList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("get getAdvCommentList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delAdvisoryInfo(int advId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //获得所有的咨询信息
            advisoryinfoDao.delAdvisoryinfo(advId);

            //获得用户所有的咨询信息后返回的json数据
            jsonUtil.setMsg("del user advisoryinfo success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(advId);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>getAdvisoryInfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("del user advisoryinfo flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAdvinfoByAreacodeandCropid(String areacode,String subjectid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            List<Advisoryinfo> adv = advisoryinfoDao.getAdvinfoByAreacodeandCropid(areacode, subjectid);
            jsonUtil.setMsg("getAdvinfoByAreacodeandCropid  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(adv);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>getAdvinfoByAreacodeandCropid=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAdvinfoByAreacodeandCropid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAdvAllInfoByUserIdorAreaCode(Table table,String userId,String areaCode,String subjectId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        List<AdvAllInfo> advAllList=new ArrayList<>();
        int count;
        try{
            if(areaCode!=null || subjectId!=null ){
//                areaCode=areaCode+"%";
                advAllList=advisoryinfoDao.getAdvAllInfoByareaCode(table,areaCode,subjectId);
                count=advisoryinfoDao.getAdvAllCountByAreaCode(table,areaCode,subjectId);
            }else {
                advAllList = advisoryinfoDao.getAdvAllInfoByUserId(table, userId);
                count = advisoryinfoDao.getAdvAllCountByUserId(table, userId);
            }
            if(advAllList.size()>0){
                for(AdvAllInfo advAllInfo:advAllList){
                    Integer advInfoId = advAllInfo.getId();
                    //该咨询的点赞总数
                    int praiseNums=advisoryinfoDao.getAdvpraiseCount(advInfoId);
                    advAllInfo.setPraiseNums(praiseNums);
                    //登陆者是否对该咨询点赞
                    String isPraise = advisoryinfoDao.getAdvpraise(Integer.valueOf(userId), advInfoId);
                    if(isPraise==null || isPraise.equals("0")){
                        advAllInfo.setIsPraise(0);//未点赞
                    }else{
                        advAllInfo.setIsPraise(1);//已点赞
                    }
                    //查出该咨询的评论内容
                    List<AdvComment> commentList = advAllInfo.getCommentList();
                    List<AdvComment> advCommentList = advisoryinfoDao.getAdvcommentListByAdvInfoId(advInfoId);
                    for(AdvComment a:advCommentList){
                        if(a.getParentId()==-1)commentList.add(a);
                    }
                    getAdvCommentList(advCommentList, commentList);

                }
            }
            table.setCount(count);
            table.setData(advAllList);
            jsonUtil.setMsg("getAdvAllInfoByUserId  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>getAdvAllInfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAdvAllInfoByUserId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAdvByuserIdAndSubject(String userId, String subjectid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            List<Advisoryinfo> adv = advisoryinfoDao.getAdvByuserIdAndSubject(userId, subjectid);

            jsonUtil.setMsg("getAdvByuserIdAndSubject  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(adv);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AdvisoryinfoServiceImpl=>getAdvByuserIdAndSubject=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getAdvByuserIdAndSubject flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    //递归(根据ParentId和对应的ID得到List)
    private void getAdvCommentList(List<AdvComment> list, List<AdvComment> advComments) {
        if(advComments.size()!=0) {
            for (AdvComment a : advComments) {
                List<AdvComment> advcommentlist=new ArrayList<>();
                for (AdvComment b : list) {
                    if (b.getParentId() == a.getId()) {
                        advcommentlist.add(b);
                    }
                }
                getAdvCommentList(list,advcommentlist);
                a.setChildrenNode(advcommentlist);
            }
        }
    }
}
